<?php

namespace common\models\shop;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%shop_activity_product_group}}".
 *
 * @property int $id
 * @property int $activity_id 活动ID
 * @property string $title
 * @property string $description
 * @property string $thumb
 * @property int $listorder
 * @property int $column
 */
class ShopActivityProductGroup extends \common\db\ActiveRecord {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%shop_activity_product_group}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['activity_id', 'listorder', 'column'], 'integer'],
            [['title'], 'required'],
            [['title'], 'string', 'max' => 50],
            [['description', 'thumb'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'activity_id' => '活动ID',
            'title' => 'title',
            'description' => 'Description',
            'thumb' => 'Thumb',
            'listorder' => 'Listorder',
            'column' => 'column',
        ];
    }

    public function getActivity() {
        return $this->hasOne(ShopActivity::className(), ['id' => 'activity_id']);
    }

    public function getProducts() {
        return $this->hasMany(ShopActivityProduct::className(), ['group_id' => 'id']);
    }

    /**
     * @inheritdoc
     */
    public function fields() {
        return [
            'id',
            'title',
            'thumb' => function() {
                return Url::attach($this->thumb);
            },
            'description',
            'column',
            'products',
            'weblink' => function() {
                return Url::weblink('activity/group', [
                    'id' => $this->id
                ]);
            },
        ];
    }

}
